<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
        .talk_con{
            width: 600px;
            height: 500px;
            border: 1px solid #666666;
            margin: 50px auto 0;
            background: #f9f9f9;
        }
        .talk_show{
            width: 580px;
            height: 420px;
            border: 1px solid #666666;
            margin: 10px auto 0;
            background: #ffffff;
            overflow: auto;
        }
        .talk_input{
            width: 580px;
            margin: 10px auto 0;
        }
        .whotalk{
            width: 80px;
            height: 30px;
            float: left;
            outline: none;
        }
        .talk_word{
            width: 420px;
            height: 26px;
            padding: 0px;
            float: left;
            margin-left: 10px;
            outline: none;
            text-indent: 10px;
        }
        .talk_sub{
            width: 56px;
            height: 30px;
            float: left;
            margin-left: 10px;
        }
        .atalk{
            margin: 10px;
        }
        .atalk span{
            display: inline-block;
            background: #0181cc;
            border-radius: 10px;
            color: #ffffff;
            padding: 5px 10px;
        }
        .btalk{
            margin: 10px;
            text-align: right;
        }
        .btalk span{
            display: inline-block;
            background: #ef8201;
            border-radius: 10px;
            color: #ffffff;
            padding: 5px 10px;
        }
    </style>
    <script type="text/javascript">
        window.onload = function () {
            var oWord = document.getElementById('words');
            var oWho = document.getElementById('who');
            var oTalk = document.getElementById('talkword');
            var oBtn = document.getElementById('talksub');

            oBtn.onclick = function () {
                // 获取下拉框的value属性值
                var sVal01 = oWho.value;
                // alert(sVal01);

                // 获取单行文本输入框中的内容
                var sVal02 = oTalk.value;
                oTalk.value = '';
                // alert(sVal02);

                // 判断输入框是否为空
                if(sVal02 == '') {
                    alert('请输入内容');
                    return;
                }

                // 拼接结构的字符串
                var sTr = '';
                if(sVal01 == 0) {
                    sTr = '<div class="atalk"><span>A说：' + sVal02 + '</span></div>';
                } else {
                    sTr = '<div class="btalk"><span>B说：' + sVal02 + '</span></div>';
                }

                // 将结构的字符串放到聊天记录的容器标签中
                oWord.innerHTML = oWord.innerHTML + sTr;
            }
        }
    </script>
</head>
<body>
    <div class="talk_con">
        <div class="talk_show" id="words">
            <div class="atalk"><span>A说：我喜欢你</span></div>
            <div class="btalk"><span>B说：啊？</span></div>
        </div>
        <div class="talk_input">
            <select name="" id="who" class="whotalk">
                <option value="0">A说：</option>
                <option value="1">B说：</option>
            </select>
            <input type="text" class="talk_word" id="talkword">
            <input type="button" value="发送" class="talk_sub" id="talksub">
        </div>
    </div>
</body>
</html>
